﻿Imports System.Data
Imports log4net

Partial Class mtCmmtHierarchy_Add
    Inherits System.Web.UI.Page

    Public AlertText As String
    Public ScriptText As String
    Private Shared logger As ILog = LogManager.GetLogger("mtCmmtHierarchy_Add")

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        If Session("users") Is Nothing Then
            ScriptText = "<script> window.parent.TINY.box.hide();</script>"
        End If
        'Session("users") = "bb0011"
        If Page.IsPostBack = False Then
            Label1.Text = " เพิ่ม Commitment Item"
            Dim adapter As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
            Dim datatable As New MasterDataSet.CmmtHierarchiesDataTable
            datatable = adapter.GetDataByID(Request("CmmCode"))
            Dim level As Decimal
            For Each row As MasterDataSet.CmmtHierarchiesRow In datatable
                ParentLabel.Text = row.Description
                level = row.Level
            Next
            If level = 1 Then
                GroupName.Visible = True
                WebCombo1.Visible = False
                Label2.Text = "ชื่อกลุ่ม"
            Else
                GroupName.Visible = False
                WebCombo1.Visible = True
            End If
        End If

    End Sub

    Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs)
        Dim adap As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim dt As New MasterDataSet2.CmmtsDataTable
        Dim planyear = Val(Request("PlanYear"))
        dt = adap.GetDataByPlanYear(planyear)

        e.DataSource = dt
    End Sub

    Protected Sub ImageButton1_Click(sender As Object, e As ImageClickEventArgs) Handles ImageButton1.Click
        Dim cmmCode As String
        Dim parent_level As Integer
        Dim level As Integer = 0
        Dim description As String
        Dim HierarchieID As Decimal = 0
        Dim adapter As New MasterDataSetTableAdapters.CmmtHierarchiesTableAdapter
        Dim datatable As New MasterDataSet.CmmtHierarchiesDataTable
        Dim f_adapter As New MasterDataSet2TableAdapters.CmmtsTableAdapter
        Dim f_datatable As New MasterDataSet2.CmmtsDataTable
        If GroupName.Visible = True Then
            adapter.InsertQuery(Val(Request("PlanYear")), Request("Group"), 2, GroupName.Text, Now(), Session("users"), Nothing, Val(Request("PlanYear")), Request("CmmCode"))
            logger.Info("User " & Session("users") & " insert commitment " & Request("CmmCode") & " in group " & Request("Group"))
        Else
            cmmCode = WebCombo1.Value
            Dim exist As Integer
            exist = adapter.CheckExist(WebCombo1.Value, Request("Group"), Val(Request("PlanYear")))
            If exist > 0 Then
                Alert("ปรากฎ Commitment Item นี้ในโครงสร้างแล้ว ไม่สามารถบันทึกข้อมูลได้")
                Exit Sub
            End If

            adapter.InsertQuery(Val(Request("PlanYear")), Request("Group"), 3, Nothing, Now(), Session("users"), cmmCode, Val(Request("PlanYear")), Request("CmmCode"))
            logger.Info("User " & Session("users") & " insert commitment " & Request("CmmCode") & " in group " & Request("Group"))
        End If

        ScriptText = "<script> window.parent.TINY.box.hide();</script>"
    End Sub

    Sub Alert(ByVal message As String)
        AlertText = "<script> alert('" & message & "');</script>"
    End Sub

    'Protected Sub SaveBtn_Click(sender As Object, e As ImageClickEventArgs) Handles SaveBtn.Click
    '    WebGrid1.ClearCachedDataSource()
    '    WebGrid1.RebindDataSource()
    'End Sub

End Class
